<?php
# Sample script for RAWS tutorial 6: see http://rambla.eu/tutorial-6-handling-atom-links-and-pagination-php for more info.
#
# Copyright (C) 2010 rambla.be

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require_once '../RawsClient/Raws/Rass.php';

# Provide the path to a local testfile, that can be uploaded via RASS
define('LOCAL_FILE', '/my/path/to/a/file.mp4');

# Provide your own RASS credentials here
define('USER', 'xxx'); # your user account name
define('PWD', 'xxx'); # your user account pwd
define('CDN', "cdn0x"); # your sub-cdn (e.g. "cdn01")

define('BASE', 'http://rass.' . CDN . '.rambla.be/');

# all RASS API methods (except HEAD) will raise an exception if they don't return an HTTP SUCCESS CODE (200,201,204)
try {
  # Instantiate an object that manages the RASS connection, passing it your login credentials and the base service uri
  $rass = new Rass(USER, PWD, BASE);

  ## TUTORIAL PRE-CONFIG: FIRST CREATE DIRECTORY + UPLOAD FILES TO HAVE SOME TEST-DATA

  # Create a "tutorial6" directory under the root-directory
  $item_entry = null;
  $dir_entry = $rass->createDir("tutorial6", True);
  echo "Created directory with path: " . $dir_entry->path . "\n";
  $ctr = 1;
  # Upload the local file 15 times as 'testXX.mp4' to the "tutorial6" directory (creating some test data)
  while ($ctr < 16) {
    $item_entry = $rass->postItem($dir_entry->path, "test$ctr.mp4", LOCAL_FILE);
    echo "Created file with path: " . $item_entry->path . "\n";
    $ctr += 1;
  }
  
  
  ## GETTING LINK ELEMENTS FOR AN ENTRY
  
  # Directly getting at an item entry's link elements
  echo "\nGetting all link elements for entry with path: " . $item_entry->path . "\n";
  foreach($item_entry->link as $link) {
    echo "\nType of relation: " . $link->rel;
    echo "\nLink URI: " . $link->href;
    echo "\nExpected type: " . $link->type . "\n";
  }
  
  # Using a helper method to get to an entry's enclosure URL
  echo "\nGetting self link via helper method: " . $item_entry->get_self_url();
  echo "\nGetting alternate link via helper method: " . $item_entry->get_alternate_url();
  echo "\nGetting edit link via helper method: " . $item_entry->get_edit_url();
  echo "\nGetting enclosure link (= download location of the file on the CDN) via helper method: " . $item_entry->get_enclosure_url() . "\n";
  
  
  ## GETTING LINK ELEMENTS FOR A FEED  
  
  # Retrieve a list of the files inside the 'tutorial6' sub-directory
  $query = new Rass_DirQuery("file");
  $feed = $rass->getDirFeed($dir_entry->path, $query);
  
  # Directly getting at the item entry's link elements
  echo "\nGetting all link elements for feed with ID: " . $feed->id . "\n";
  foreach($feed->link as $link) {
    echo "\nType of relation: " . $link->rel;
    echo "\nLink URI: " . $link->href;
    echo "\nExpected type: " . $link->type . "\n";
  }
  
  
  ## USING PAGINATION (via helper functions)
  
  # Since we've uploaded 15 files, we can retrieve partial results by suggesting to RASS that we only want 10 results in a single response
  $query->setPaginateBy("10");
  $feed = $rass->getDirFeed($dir_entry->path, $query);
  
  # since the request only contains 10 entries, there should be a 'next' + 'last' link inside the feed element
  echo "\nNext link URI: " . $feed->get_next_link() . "\n";
  echo "Last link URI: " . $feed->get_last_link() . "\n";
  
  # There's also a helper function to directly get at the 'next' feed (takes the original feed as argument)
  $next_feed = $rass->getNextFeed($feed);
  echo "\nThis feed contains all remaining entries => new call to get_next_link() will return empty string: " . $next_feed->get_next_link() . "\n";
  

  ## PAGINATION BEST PRACTICES

  # if you want to retrieve all results from a resource that supports pagination, you might want to use a while loop
  echo "\nGetting all files under " . $dir_entry->path . " using pagination:\n";
  $feed = $rass->getDirFeed($dir_entry->path, $query);
  while($feed)
  {
    foreach ($feed as $entry) {
      # process your entries here..
      echo "\nFound entry with path = " . $entry->path;
    }
    # get next feed, by sending a new request to the next link inside this page
    $feed = $rass->getNextFeed($feed);
    if ($feed) {
      echo "\nGetting next page..";
    }
  }


  ## TUTORIAL CLEANUP

  # cleanup the tutorial6 dir
  $rass->deleteDir($dir_entry->path, True);
  echo "\nDeleted dir: " . $dir_entry->path . "\n";
    
}
catch(Zend_Gdata_App_Exception $e) {
    # Report the exception to the user
    echo "\nCaught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
    # get the HTTP status code
    echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
    echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
    # get the raws:error elements
    $rawsResponse = Raws::parseRawsResponse($e->getResponse());
    echo "Raws Code: " . $rawsResponse->getCode() . "\n";
    echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
    $reasons = $rawsResponse->getReasons();
    foreach ($reasons as $reason) {
      echo "Raws Reason: " . $reason . "\n";
    }
    $details = $rawsResponse->getDetails();
    foreach ($details as $key => $value) {
      echo "Raws Detail: " . $key . " -> " . $value . "\n";
    }
}
catch (Zend_Exception $e) {
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
}
